package org.springframework.context.annotation;

import cn.hutool.core.util.ClassUtil;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.stereotype.Compenent;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @Name ClasspathScanningCandidateComponentProvider
 * @Description: @Component 注解扫描器
 * 描述信息：扫描指定包中 @Component 注解的类，提取类信息，组装成 BeanDefinition。
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-04-09 16:33:04
 **/
public class ClasspathScanningCandidateComponentProvider {

    public Set<BeanDefinition> findCandidateComponents(String basePackage) {
        Set<BeanDefinition> candidates = new LinkedHashSet<>();
        // 扫描所有 org.springframework.stereotype.Compenent 注解的类
        Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation(basePackage, Compenent.class);
        for (Class<?> clazz : classes) {
            BeanDefinition beanDefinition = new BeanDefinition(clazz);
            candidates.add(beanDefinition);
        }
        return candidates;
    }

}

